package com.webdesign.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.opensymphony.xwork2.ActionContext;
import com.webdesign.dao.mapper.TbAdminMapper;
import com.webdesign.dao.mapper.TbTeacherMapper;
import com.webdesign.entity.TbAdmin;
import com.webdesign.entity.TbTeacher;
import com.webdesign.entity.TbTeacherExample;
import com.webdesign.service.ITbTeacherService;
import com.webdesign.util.MD5;
import com.webdesign.util.SendMailUtil;

@Service("TbTeacherService")
public class TbTeacherService implements ITbTeacherService {
	@Autowired
	private TbTeacherMapper tbTeacherMapper;
	@Autowired
	private TbAdminMapper tbAdminMapper;

	/*
	 * 教师登录
	 */
	@Override
	public TbTeacher login(String userid, String password) {
		// TODO Auto-generated method stub
		TbTeacher tbTeacher = tbTeacherMapper.selectByPrimaryKey(userid);
		if(tbTeacher != null){
			if(tbTeacher.getState().equals("1"))
			{
				if(tbTeacher.getPassword().equals(password)){
					return tbTeacher;
				}else{
					TbTeacher teacher = null;
					return teacher;
				}
			}else{
				TbTeacher teacher = null;
				return teacher;
			}
		}else{
			TbTeacher teacher = null;
			return teacher;
		}
	}

	/*
	 * (clover 4.11)教师修改密码
	 */
	@Override
	public TbTeacher rewrite(String userid, String password, String newpwd) {
		// TODO Auto-generated method stub
		TbTeacher tbTeacher = tbTeacherMapper.selectByPrimaryKey(userid);
		if(tbTeacher != null){
			if(tbTeacher.getPassword().equals(password)){
				TbTeacher teacher = new TbTeacher();
				teacher.setUsername(tbTeacher.getUsername());
				teacher.setPassword(newpwd);
				teacher.setEmail(tbTeacher.getEmail());
				teacher.setState(tbTeacher.getState());
				teacher.setUserid(userid);
				tbTeacherMapper.updateByPrimaryKey(teacher);
				return teacher;
			}else{
				TbTeacher teacher = null;
				return teacher;
			}
		}else{
			TbTeacher teacher = null;
			return teacher;
		}
	}
	
	/*
	 * 教师注册
	 * */
	
	@Override
	public boolean register(String userid,String username,String password,String email)
	{
		// TODO Auto-generated method stub
		TbTeacher tbTeacher = tbTeacherMapper.selectByPrimaryKey(userid);
		if(tbTeacher!=null)
		{
			return false;
		}
		else
		{
			TbTeacher teacher=new TbTeacher();
			teacher.setUserid(userid);
			teacher.setUsername(username);
			teacher.setPassword(password);
			teacher.setState("0");
			teacher.setEmail(email);
			tbTeacherMapper.insert(teacher);
			return true;
		}
	}

/*
 * 显示未审核教师
 */
	@Override
	public List<TbTeacher> showteacher() {
		// TODO Auto-generated method stub
		TbTeacherExample tbTeacherExample = new TbTeacherExample();
		tbTeacherExample.createCriteria().andStateEqualTo("0");
		List<TbTeacher> tbTeachers = tbTeacherMapper.selectByExample(tbTeacherExample);
		for(TbTeacher tbTeacher : tbTeachers)
		{
			tbTeacher.setPassword(null);
			tbTeacher.setEmail(null);
		}
		return tbTeachers;
	}
	
	/*
	 * 修改教师密码页面显示
	 */
	@Override
	public List<TbTeacher> resetTeachersPwdShow(){
		
		TbTeacherExample tbTeacherExample = new TbTeacherExample();
		tbTeacherExample.createCriteria().andStateEqualTo("1");
		List<TbTeacher> teachers = tbTeacherMapper.selectByExample(tbTeacherExample);
		
		return teachers;
	}
	
	/*
	 * 重置教师密码功能实现
	 */
	@Override
	public Boolean resetTeacherPwd(String[] teacherid){
		
		MD5 pwd =new MD5();
		Boolean flag=true;
		for(int i = 0;i < teacherid.length;i++)
		{
			TbTeacher teacher=tbTeacherMapper.selectByPrimaryKey(teacherid[i]);
			TbTeacher resetTeacher = new TbTeacher();
			String resetPassword = pwd.MD5(teacher.getUserid());
			
			//重置后教师的新密码和工号一致
			resetTeacher.setUserid(teacher.getUserid());
			resetTeacher.setUsername(teacher.getUsername());
			resetTeacher.setPassword(resetPassword);
			resetTeacher.setState(teacher.getState());
			resetTeacher.setEmail(teacher.getEmail());
						
			if(tbTeacherMapper.updateByPrimaryKey(resetTeacher)==0)
			{
				flag=false;
			}
		}
		return flag;
	}

	/*
	 * 教师审核通过
	 */
	@Override
	public void checkTeacher(String teacherid)
	{
		// TODO Auto-generated method stub
		TbTeacher tbteacher=new TbTeacher();
		tbteacher.setState("1");
		tbteacher.setUserid(teacherid);
		if(tbTeacherMapper.updateByPrimaryKeySelective(tbteacher)==1){
			try {
				Map<String,Object> session = ActionContext.getContext().getSession();
				String username = (String) session.get("username");
				TbAdmin tba = tbAdminMapper.selectByPrimaryKey(username);
				String from = tba.getEmail();
				String mailpwd = tba.getEmailpassword();
				TbTeacher tbt = tbTeacherMapper.selectByPrimaryKey(teacherid);
				String to = tbt.getEmail();
	        	SendMailUtil.sendEmail(from,mailpwd,to,"您已通过账号注册审核！");
	        }catch (Exception e) {
	            e.printStackTrace();
	        }
		}
	}

	/*
	 * 教师审核不通过
	 */
	@Override
	public Boolean deleteTeacher(String teacherid) 
	{
		Map<String,Object> session = ActionContext.getContext().getSession();
		String username = (String) session.get("username");
		TbAdmin tba = tbAdminMapper.selectByPrimaryKey(username);
		String from = tba.getEmail();
		String mailpwd = tba.getEmailpassword();
		TbTeacher tbt = tbTeacherMapper.selectByPrimaryKey(teacherid);
		String to = tbt.getEmail();
		if(tbTeacherMapper.deleteByPrimaryKey(teacherid) == 1)
		{
			try {
	        	SendMailUtil.sendEmail(from,mailpwd,to,"账号注册失败，请重新申请！");
	        }catch (Exception e) {
	            e.printStackTrace();
	        }
			return true;
		}
		
		return false;
	}

	/*
	 * 显示通过审核教师
	 */
	@Override
	public List<TbTeacher> showPassTeacher() {
		// TODO Auto-generated method stub
		TbTeacherExample tbTeacherExample = new TbTeacherExample();
		tbTeacherExample.createCriteria().andStateEqualTo("1");
		List<TbTeacher> tbTeachers = tbTeacherMapper.selectByExample(tbTeacherExample);
		for(TbTeacher tbTeacher : tbTeachers)
		{
			tbTeacher.setPassword(null);
			tbTeacher.setEmail(null);
		}
		return tbTeachers;
	}
}
